package com.imay.live.manager;

import android.content.Context;
import android.text.TextUtils;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.imay.live.IMApplication;
import com.imay.live.utils.AppUtil;
import com.imay.live.R;

import java.io.IOException;
import java.util.List;

/**
 * Created by  nick on 2017/5/19 0019.
 * 敏感词过滤
 */

public class SensitiveWordFilterManager {

    private List<String> sensitiveWords ;

    private static Context applicationContext;

    private static class SensitiveWordFilterManagerHolder{
        private static final SensitiveWordFilterManager INSTANCE = new SensitiveWordFilterManager();
    }

    public static final SensitiveWordFilterManager getInstance(){
        return SensitiveWordFilterManagerHolder.INSTANCE;
    }

    private SensitiveWordFilterManager(){
        try {
            sensitiveWords =  JSON.parseArray(AppUtil.getFromRaw(IMApplication.getInstance(), R.raw.sensitive_words), String.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public  boolean hasSensitiveWord(String word){
        boolean hasSensitive = false;
        if (sensitiveWords==null){
            return hasSensitive;
        }
        for (String key:sensitiveWords) {
            if (word.contains(key)){
                hasSensitive = true;
                break;
            }
        }
        return hasSensitive;
    }
    public  boolean hasSensitiveWord(EditText editText){
        boolean hasSensitive = false;
        if (sensitiveWords==null||TextUtils.isEmpty(editText.getText())){
            return hasSensitive;
        }
        String text = editText.getText().toString();
        for (String key:sensitiveWords) {
            if (text.contains(key)){
                hasSensitive = true;
                break;
            }
        }
        return hasSensitive;
    }
}
